import React from "react";
import {observer} from "mobx-react";
import {Modal, Tools, List, InputItem, Toast, Bubble} from "weaver-mobile";
import {suppoertCAAuth, getCAAuthInfo, checkCAAuth} from "../../util/CAJsApi";
import {saveCAAuthResult} from "../../apis/secondAuth";

const getLabel = Tools.getLabel;
const Item = List.Item;

const initalState = {
    owner: "",              //证书信息：所有者
    startTime: "",          //证书信息：开始时间
    endTime: "",            //证书信息：结束时间
    signAlg: ""            //证书信息：加密算法
};

@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {...initalState};
    }


    componentDidMount() {
        const {visible} = this.props;
        if (visible) {
            this.getCACert();
        }
    }

    componentWillReceiveProps(nextProps, nextContext) {
        const visible1 = this.props.visible;
        const visible2 = nextProps.visible;

        if (visible1 != visible2 && visible2) {
            this.getCACert();
        }
    }

    getCACert() {
        if (suppoertCAAuth()) {
            getCAAuthInfo(data => this.setCAAuthInfo(data));
        } else {
            const errorMsg = getLabel(501212, "您当前的客户端不支持CA认证");
            Toast.fail(errorMsg, 2);
        }
    }

    setCAAuthInfo(data) {
        if (typeof data == "string") {
            data = JSON.parse(data);
        }
        const errCode = data.errCode;
        const errMsg = data.errMsg;
        const caCertInfo = data.getCACertInfo;

        if (errCode == 0) {       //成功
            if (caCertInfo) {
                const state = this.state;
                this.setState({...state, ...caCertInfo});
            }
        } else {
            if (!!errMsg) {
                Toast.fail(errMsg, 2);
            } else {
                Toast.fail(getLabel(501213, "获取CA证书信息失败"), 2);
            }
        }
    }

    onChangeEvt(val) {
        const {secondAuthStore} = this.props;
        secondAuthStore.saveAuthCode(val);
    }

    doSubmit() {
        if (suppoertCAAuth()) {
            const {secondAuthStore} = this.props;
            const {authCode, sourcestr} = secondAuthStore;

            if (!authCode) {
                Bubble.error(getLabel(501214, "请输入证书密码"), 2);
            } else {
                checkCAAuth(sourcestr, authCode, data => this.submitBack(data));
            }
        } else {
            const errorMsg = getLabel(501212, "您当前的客户端不支持CA认证");
            Toast.fail(errorMsg, 2);
        }
    }

    submitBack(data) {
        const {secondAuthStore} = this.props;
        const {secondAuthType, isProtectDialog, authKey} = secondAuthStore;
        if (typeof data == "string") {
            data = JSON.parse(data);
        }
        let signValue = "";
        if (!!data.getSignValue) {
            const result = data.getSignValue;
            if (!!result) {
                signValue = result.signValue;
            }
        }

        if (!signValue) {
            Bubble.error(getLabel(501215, "数据签名失败，未获取到签名后的值"), 2);

            if (this.props.onFail) {
                this.props.onFail.call({}, {isProtectDialog});
            }
        } else {
            saveCAAuthResult({signValue, authKey}).then(response => {
                let success = response.success;
                if (success == 1) {
                    if (this.props.onSuccess) {
                        this.props.onSuccess.call({}, {isProtectDialog});
                    }
                } else {
                    Bubble.error(getLabel(389490, "认证失败"), 2);
                }
            })
        }
    }

    closeDialog() {
        if (this.props.onClose) {
            this.props.onClose.call({});
        }
    }

    render() {
        const {secondAuthStore, visible} = this.props;
        const {owner, startTime, endTime, signAlg} = this.state;
        const title = getLabel(501216, "CA证书验证");

        const footer = [
            {text: getLabel(501196, "确定"), onPress: this.doSubmit.bind(this)},
            {text: getLabel(31129, "取消"), onPress: this.closeDialog.bind(this)},
        ];

        return (
            <Modal
                visible={visible}
                transparent={true}
                maskClosable={false}
                onClose={this.closeDialog.bind(this)}
                title={title}
                footer={footer}
                className="sencond-auth-dialog"
            >
                <List>
                    <InputItem className="ca-auth-item"
                               value={owner}
                               editable={false}>
                        {getLabel(79, "所有者")}
                    </InputItem>
                    <InputItem className="ca-auth-item"
                               value={startTime}
                               editable={false}>
                        {getLabel(501478, "颁发时间")}
                    </InputItem>
                    <InputItem className="ca-auth-item"
                               value={endTime}
                               editable={false}>
                        {getLabel(501479, "失效时间")}
                    </InputItem>
                    <InputItem className="ca-auth-item"
                               value={signAlg}
                               editable={false}>
                        {getLabel(32344, "加密算法")}
                    </InputItem>
                    <InputItem className="ca-auth-inputitem"
                               placeholder={getLabel(501214, "请输入证书密码")}
                               type="password"
                               onChange={this.onChangeEvt.bind(this)}/>
                </List>
            </Modal>
        );
    }
}
